package cn.tedu.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * @Author: Miyako
 * @Date: 2024-07-30-11:03
 * @Description: 连接数据库测试，用jdbc插入表记录
 */
public class JDBCDemo01 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //通过反射在程序运行时加载驱动类
        Class.forName("com.mysql.cj.jdbc.Driver");
        //创建数据库连接对象 ：参数1：url连接 2.用户名 3.密码
        Connection connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306" +
                        "/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia" +
                        "/Shanghai&rewriteBatchedStatements=true",
                "root",
                "root"
        );
        // 创建sql语句执行对象
        Statement statement = connection.createStatement();
        //编写sql语句
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入用户名");
        String username = scanner.nextLine();
        System.out.println("输入密码");
        String password = scanner.nextLine();
        System.out.println("输入昵称");
        String nickname = scanner.nextLine();
        System.out.println("输入年龄");
        int age = scanner.nextInt();

        String sql = "INSERT INTO userinfo(username,password,nickname,age) " +
                "VALUES("+"'"+username+"','"+password+"','"+nickname+"','"+age+"')";

//        String sql = "INSERT INTO userinfo(username,password,nickname,age) VALUES('Tony','123456','tony','65')";
//        String sql = "delete from userinfo where username='Tony'";
        //执行sql语句，然后返回一个整型其值是受影响的数据条数

            int j = statement.executeUpdate(sql);
        System.out.println(j);
        //断开数据库连接
        connection.close();
    }
}
